﻿using System;
using System.Web.Services;
using DAL;
using NLog;
using Entity;
using System.Xml;
using System.Data;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

public partial class Admin_Articles : ParentPage
{

    private static Logger _logger = LogManager.GetCurrentClassLogger();

    #region Pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                ManageArticls objDALManageArticls = new ManageArticls();
                rptManageCategory.DataSource = objDALManageArticls.GetAllArticls();
                rptManageCategory.DataBind();

                Users objUser = (Users)Session["Admin"];
                if (objUser.UserPermission == 1)
                {
                    btnRSSFeedsGeneraters.Visible = false;
                    btnPingGoogleSiteMap.Style.Add("display", "none");
                }
            }

        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Articles:::Page_Load:::" + ex.Message);
        }
    }
    #endregion

    #region Events
    protected void lbtnRefreshGV_Click(object sender, EventArgs e)
    {
        try
        {
            ManageArticls objDALManageArticls = new ManageArticls();
            rptManageCategory.DataSource = objDALManageArticls.GetAllArticls();
            rptManageCategory.DataBind();
        }
        catch (Exception ex)
        {
            // _logger.Error("Admin_Articles:::lbtnRefreshGV_Click:::" + ex.Message);
        }
    }
    protected void btnRSSFeedsGeneraters_Click(object sender, EventArgs e)
    {
        try
        {
            RSSFeedsGeneraters objRSSFeedsGeneraters = new RSSFeedsGeneraters();
            objRSSFeedsGeneraters.DoRSSGenerater();
            objRSSFeedsGeneraters.DoRSSGeneraterForAndroidApps();
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Articles:::btnRSSFeedsGeneraters_Click:::" + ex.Message);
        }
    }
    protected void btnPingGoogleSiteMap_Click(object sender, EventArgs e)
    {
        try
        {
            System.Net.WebRequest reqGoogle = System.Net.WebRequest.Create("http://www.google.com/webmasters/tools/ping?sitemap=" + HttpUtility.UrlEncode("http://www.livekora.net/sitemap/sitemap.xml"));
            reqGoogle.GetResponse();


            System.Net.WebRequest reqBing = System.Net.WebRequest.Create("http://www.bing.com/webmaster/ping.aspx?siteMap=" + HttpUtility.UrlEncode("http://www.livekora.net/sitemap/sitemap.xml"));
            reqBing.GetResponse();

        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Articles:::btnPingGoogleSiteMap_Click:::" + ex.Message);
        }
    }
    #endregion

    #region Methods

 

    /// <summary>
    /// Convert Category Status from Number to text
    /// </summary>
    /// <param name="sCategoryStatus">set Category Status "string value"</param>
    /// <returns>return Category status</returns>
    protected string CheckCategoryID(string sCategoryID)
    {
        try
        {
            ManageCategory objDALManageCategory = new ManageCategory();
            Category objCategory = objDALManageCategory.GetCategoryInfo(sCategoryID);
            return objCategory.CategoryName;
        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    /// <summary>
    /// SplitString
    /// </summary>
    /// <param name="sCategoryStatus">set StringValue Status "string value"</param>
    /// <returns>return StringValue</returns>
    protected string SplitString(string StringValue)
    {
        try
        {
            if (StringValue.Length >= 40)

                return StringValue.Substring(0, 39) + "...";
            else
                return StringValue;

        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    /// <summary>
    /// to get TinyURL 
    /// </summary>
    /// <param name="longURL">set URL "string value"</param>
    /// <returns>return TinyURL otherwise return unknown</returns>
    public string getTinyURL(string longURL)
    {
        string shortUrl = string.Empty;
        try
        {
            string tinyURLApiUrl = "http://tinyurl.com/api-create.php?url={0}";
            tinyURLApiUrl = tinyURLApiUrl.Replace("{0}", longURL);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tinyURLApiUrl);
            WebResponse response = request.GetResponse();
            Stream responseStream = request.GetResponse().GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            shortUrl = reader.ReadToEnd();
            return shortUrl;
        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    #endregion

    #region WebMethods

    [WebMethod]
    public static string DeleteArticlesInfo(string sArticlesID)
    {
        try
        {
            ManageArticls objDALCategory = new ManageArticls();
            int nReturnValue = objDALCategory.DeleteArticls(sArticlesID);
            if (nReturnValue == 1)
                return "true";
            else
                return "false";
        }
        catch (Exception ex)
        {
            _logger.Error("Admin_Articles:::DeleteArticlesInfo:::" + ex.Message);
            return "false";
        }
    }
    #endregion

}